package top.wardyan.wdev.dbassist.config;

import java.util.Properties;

import com.jfinal.kit.PropKit;

import top.wardyan.base.config.Datasource;
import top.wardyan.base.config.SystemConfig;
import top.wardyan.base.db.BaseSQLServerDatasource;
import top.wardyan.wdev.dbassist.model.YTDic;
//import top.wardyan.wdev.dbassist.service.IMasterService;
import top.wardyan.wdev.dbassist.model.YTDicItem;
import top.wardyan.wdev.dbassist.model.YTFunc;
import top.wardyan.wdev.dbassist.model.YTFuncCol;
import top.wardyan.wdev.dbassist.model.YTFuncPara;
import top.wardyan.wdev.dbassist.model.YTIndex;
import top.wardyan.wdev.dbassist.model.YTIndexCol;
import top.wardyan.wdev.dbassist.model.YTLog;
import top.wardyan.wdev.dbassist.model.YTProc;
import top.wardyan.wdev.dbassist.model.YTProcCol;
import top.wardyan.wdev.dbassist.model.YTProcPara;
import top.wardyan.wdev.dbassist.model.YTTable;
import top.wardyan.wdev.dbassist.model.YTTableCol;
import top.wardyan.wdev.dbassist.model.YTTableFunc;
import top.wardyan.wdev.dbassist.model.YTTableFuncCol;
import top.wardyan.wdev.dbassist.model.YTTableFuncPara;
import top.wardyan.wdev.dbassist.model.YTTrigger;
import top.wardyan.wdev.dbassist.model.YTView;
import top.wardyan.wdev.dbassist.model.YTViewCol;
import top.wardyan.wdev.dbassist.model._MappingKit;
import top.wardyan.wdev.dbassist.service.IDetailService;
import top.wardyan.wdev.dbassist.service.ILogService;
import top.wardyan.wdev.dbassist.service.IMasterService;
import top.wardyan.wdev.dbassist.service.IYPGetIDService;
import top.wardyan.wdev.dbassist.service.impl.DetailServiceImpl;
import top.wardyan.wdev.dbassist.service.impl.LogServiceImpl;
import top.wardyan.wdev.dbassist.service.impl.MasterServiceImpl;
import top.wardyan.wdev.dbassist.service.impl.YPGetIDServiceImpl;

public class DDA extends BaseSQLServerDatasource{
	
	public static final String APPCONFIGFILE = "conf/DBAssist_AppConfig.txt";
	public static final String SYSTEMCONFIGFILE = "conf/DBAssist_SystemConfig.txt";
	public static final String DATASOURCEFILE = "conf/DBAssist_datasource.conf";
	public static final String SQLTEMPLATE = "conf/DBAssist_sqltemplate.sql";
	
	public static final Properties PROP = PropKit.use(APPCONFIGFILE).getProperties();
	public static final SystemConfig SYSTEMCONFIG = SystemConfig.getInstance(SYSTEMCONFIGFILE);
	private static Datasource DATASOURCE;
	
	private static final String ADDRESS = getAddress();
	private static final Integer PORT = getPort();
	private static final String DBNAME = getDBName();
	private static final String USERNAME = getUsername();
	private static final String PASSWORD = getPassword();
	
	public top.wardyan.base.service.IMasterService<YTDic> dicService = new top.wardyan.base.service.impl.MasterServiceImpl<>(YTDic.dao);
	public top.wardyan.base.service.IDetailService<YTDicItem> dicItemService = new top.wardyan.base.service.impl.DetailServiceImpl<>(YTDicItem.dao);
	
	public IMasterService<YTFunc> funcService = new MasterServiceImpl<>(YTFunc.dao);
	public IDetailService<YTFuncCol> funcColService = new DetailServiceImpl<>(YTFuncCol.dao);
	public IDetailService<YTFuncPara> funcParaService = new DetailServiceImpl<>(YTFuncPara.dao);
	
	
	public IMasterService<YTIndex> indexService = new MasterServiceImpl<>(YTIndex.dao);
	public IDetailService<YTIndexCol> indexColService = new DetailServiceImpl<>(YTIndexCol.dao);
	
	public ILogService logService = new LogServiceImpl(YTLog.dao);
	
	
	public IMasterService<YTProc> procService = new MasterServiceImpl<>(YTProc.dao);
	public IDetailService<YTProcCol> procColService = new DetailServiceImpl<>(YTProcCol.dao);
	public IDetailService<YTProcPara> procParaService = new DetailServiceImpl<>(YTProcPara.dao);
	
	
	
	
	
	
	public IMasterService<YTTable> tableService = new MasterServiceImpl<>(YTTable.dao);
	public IDetailService<YTTableCol> tableColService = new DetailServiceImpl<>(YTTableCol.dao);
	
	
	public IMasterService<YTTableFunc> tableFuncService = new MasterServiceImpl<>(YTTableFunc.dao);
	public IDetailService<YTTableFuncCol> tableFuncColService = new DetailServiceImpl<>(YTTableFuncCol.dao);
	public IDetailService<YTTableFuncPara> tableFuncParaService = new DetailServiceImpl<>(YTTableFuncPara.dao);
	
	
	public IMasterService<YTView> viewService = new MasterServiceImpl<>(YTView.dao);
	public IDetailService<YTViewCol> viewColService = new DetailServiceImpl<>(YTViewCol.dao);
	
	
	public IMasterService<YTTrigger> triggerService = new MasterServiceImpl<>(YTTrigger.dao);
	
	public IYPGetIDService getIDService = new YPGetIDServiceImpl();
	
	
	
	private static Datasource getDatasource() {
		if(DATASOURCE == null) {
			DATASOURCE = Datasource.parse(DATASOURCEFILE);
		}
		// 开发时
		if(DATASOURCE == null) {
			DATASOURCE = new Datasource().setName("本地").setPwd("").setAddress("127.0.0.1").setPort(1433).setDbName("ERPDB").setUsername("sa").setPassword("Wardyan020598");
		}
		return DATASOURCE;
	}
	
	private static String getAddress() {
		if(PROP != null && PROP.getProperty("address") != null) {
			return PROP.getProperty("address");
		}else {
			return getDatasource().getAddress();
		}
	}
	private static Integer getPort() {
		if(PROP != null && PROP.getProperty("port") != null) {
			return Integer.valueOf(PROP.getProperty("port", "0"));
		}else {
			return getDatasource().getPort();
		}
	}
	private static String getDBName() {
		if(PROP != null && PROP.getProperty("dbName") != null) {
			return PROP.getProperty("dbName");
		}else {
			return getDatasource().getDbName();
		}
	}
	private static String getUsername() {
		if(PROP != null && PROP.getProperty("username") != null) {
			return PROP.getProperty("username");
		}else {
			return getDatasource().getUsername();
		}
	}
	private static String getPassword() {
		if(PROP != null && PROP.getProperty("password") != null) {
			return PROP.getProperty("password");
		}else {
			return getDatasource().getPassword();
		}
	}
	public static String getArpName() {
		if(PROP != null && PROP.getProperty("dbName") != null) {
			return PROP.getProperty("dbName");
		}else {
			return getDatasource().getDbName();
		}
	}
	private DDA() {
		//AppConfig.getInstance();
		super(ADDRESS, PORT, DBNAME, USERNAME, PASSWORD, DBNAME);
		//super("", 0, "", "", "", "");
		//this.getDp()
		this.getArp().addSqlTemplate(SQLTEMPLATE);
		
		
	}
	
	
	
	
	
	
	
	
	
	
	
	private static DDA instance = null;
	public static DDA getInstance() {
		if(instance == null) {
			instance = new DDA();
		}
		return instance;
	}
	
	public static void main(String[] args) {
		//AppConfig c = AppConfig.getInstance("AppConfig.txt");
		//System.out.println(c.get("dbName"));
		//AppConfig.getInstance();
		System.out.println(ADDRESS);
		
	}

	@Override
	protected void mapping() {
		//new _MappingKit();
		new _MappingKit().mapping(this.getArp());
	}
	@Override
	protected String getPublicKey() {
		return SYSTEMCONFIG.PublicKey;
	}

}
